package com.salim.leetcode.$155;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: clevo
 * Date: 15-7-9
 * Time: 下午10:45
 * To change this template use File | Settings | File Templates.
 */
public class MinStackList {

    List<Integer> stack = new ArrayList<Integer>();
    Integer min = null;

    public void push(int x) {
        stack.add(x);
        if(min !=null){
            if(x < min){
                min = x;
            }
        }
    }

    public void pop() {
        int temp = stack.get(stack.size()-1);
        if(min !=null && temp == min){
            min = null;
        }
        stack.remove(stack.size()-1);
    }

    public int top() {
        return stack.get(stack.size()-1);
    }

    public int getMin() {
        if(min != null){
            return min;
        }
        min = stack.get(0);
        for(int i=1;i<stack.size();i++){
            int temp = stack.get(i);
            if(temp < min){
                min = temp;
            }
        }
        return min;
    }

    public static void main(String[] args){
        MinStackList minStackList = new MinStackList();
        minStackList.push(1);
        minStackList.push(13);
        minStackList.push(5);
        System.out.println(minStackList.stack.toString());
        minStackList.push(17);
        minStackList.push(19);
        minStackList.push(-1);
        System.out.println(minStackList.getMin());
        minStackList.pop();
        System.out.println(minStackList.top());
    }
}
